package com.gxy.learn.cu.utils.tree;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 获取树
 * @author: gaoxueyong
 * @Date: 2022/9/7 15:56
 */
public class TreeUtil {
    /**
     * 获取树通用子节点
     *
     * @param id   当前父节点的id
     * @param list 所有树节点
     * @return
     */
    public static List<TreeVO> getCommonChilds(String id, List<TreeVO> list) {
        //子节点
        List<TreeVO> childList = new ArrayList<>();
        for (TreeVO department : list) {
            // 遍历所有节点，将父菜单id与传过来的id比较
            if (StringUtils.isNotBlank(department.getParntId()) && StringUtils.equals(department.getParntId(), id)) {
                childList.add(department);
            }
        }
        // 递归查询子节点的子节点
        for (TreeVO department : childList) {
            department.setChildren(getCommonChilds(department.getId(), list));
        }
        // 递归退出条件
        if (childList.size() == 0) {
            return null;
        }
        return childList;
    }
}
